package CustomUI;

import java.util.ArrayList;

import com.example.dworld3.Drawings;
import com.ydev.dworld3.R;

import DTO.DrawingDTO;
import android.app.ProgressDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class ydDrawingsListAdapter extends BaseAdapter {

	private Context context;

	ArrayList<DrawingDTO> drawings;

	public ydDrawingsListAdapter(Context context, String[] mobileValues) {
		this.context = context;
	}

	public ydDrawingsListAdapter(Context context, ArrayList<DrawingDTO> drawings) {
		this.context = context;
		this.drawings = drawings;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		View listView;

		if (convertView == null) {

			listView = new View(context);

			// get layout from mobile.xml
			listView = inflater.inflate(R.layout.drawing_list_cell, null);
			DrawingDTO d = drawings.get(position);

			TextView textView = (TextView) listView
					.findViewById(R.id.textView1);
			textView.setText(d.getName());
			/*
			 * // set value into textview TextView textView = (TextView)
			 * gridView .findViewById(R.id.grid_item_label);
			 * textView.setText(mobileValues[position]);
			 * 
			 * // set image based on selected text ImageView imageView =
			 * (ImageView) gridView .findViewById(R.id.grid_item_image);
			 * 
			 * String mobile = mobileValues[position];
			 * 
			 * if (mobile.equals("Windows")) {
			 * imageView.setImageResource(R.drawable.anchor_fixed); } else if
			 * (mobile.equals("iOS")) {
			 * imageView.setImageResource(R.drawable.anchor_pivot); } else if
			 * (mobile.equals("Blackberry")) {
			 * imageView.setImageResource(R.drawable.arrow_e_normal); } else {
			 * imageView.setImageResource(R.drawable.arrow_se_pressed); }
			 */

		} else {
			listView = (View) convertView;
		}

		return listView;
	}

	@Override
	public int getCount() {
		return drawings.size();
	}

	@Override
	public Object getItem(int position) {
		return drawings.get(position);
	}

	@Override
	public long getItemId(int position) {
		return drawings.get(position).getDid();
	}

	public int getItemDId(int position) {
		return drawings.get(position).getDid();
	}

	public Context getContext() {
		return context;
	}

	public int getItemIndexWithID(int objID) {
		DrawingDTO d;
		for (int i = 0; i < drawings.size(); i++) {
			d = drawings.get(i);
			if (d.getDid() == objID) {
				return i;
			}
		}
		return -1;

	}

	public void changeName(int objID, String string) {
		DrawingDTO d;
		for (DrawingDTO i : drawings) {
			if (i.getDid() == objID) {
				i.setName(string);
			}
		}

	}

	public void removeAt(int lastSelectedPosition) {
		drawings.remove(lastSelectedPosition);

	}

	public void removeWithID(int did) {
		int x = 0;
		for (DrawingDTO i : drawings) {
			if (i.getDid() == did) {
				break;
			}
			x++;
		}
		drawings.remove(x);
	}

}
